package com.example.volunteer.service.impl;


import com.example.volunteer.dto.LoginDTO;
import com.example.volunteer.dto.LoginResponse;
import com.example.volunteer.dto.RegisterDTO;
import com.example.volunteer.exception.CustomException;
import com.example.volunteer.mapper.AdminMapper;
import com.example.volunteer.mapper.VolunteerMapper;
import com.example.volunteer.model.Admin;
import com.example.volunteer.model.Volunteer;
import com.example.volunteer.service.AuthService;
import com.example.volunteer.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private VolunteerMapper volunteerMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public String login(LoginDTO loginDTO) {
        // 参数校验
        if (loginDTO.getEmail() == null || loginDTO.getPassword() == null) {
            throw new CustomException("邮箱和密码不能为空");
        }

        String email = loginDTO.getEmail();
        String rawPassword = loginDTO.getPassword();

        // 先尝试查找志愿者
        Volunteer volunteer = volunteerMapper.findByEmail(email);
        if (volunteer != null) {
            // 验证密码
            if (passwordEncoder.matches(rawPassword, volunteer.getPassword())) {
                // 密码匹配，生成token
                String token =tokenUtil.generateToken(volunteer.getVolunteerId(), "VOLUNTEER");
                System.out.println("志愿者登录成功");
                return "志愿者登录成功! 用户ID: " + volunteer.getVolunteerId() + " | Token: " + token;
            }
            throw new CustomException("密码错误");
        }

        // 如果不是志愿者，尝试查找管理员
        Admin admin = adminMapper.findByEmail(email);
        if (admin != null) {
            // 验证密码
            if (passwordEncoder.matches(rawPassword, admin.getPassword())) {
                // 密码匹配，生成token
                String token =tokenUtil.generateToken(admin.getAdminId(), "ADMIN");
                System.out.println("管理员登录成功");
                return "管理员登录成功! 用户ID: " + admin.getAdminId() + " | Token: " + token;
            }
            throw new CustomException("密码错误");
        }

        throw new CustomException("用户不存在");
    }

    @Override
    public void register(RegisterDTO registerDTO) {
        Volunteer volunteer = new Volunteer();
        volunteer.setVolunteerId(registerDTO.getVolunteerId());
        volunteer.setName(registerDTO.getName());
        volunteer.setPassword(passwordEncoder.encode(registerDTO.getPassword()));
        volunteer.setContact(registerDTO.getContact());
        volunteer.setEmail(registerDTO.getEmail());
        volunteer.setSkills(registerDTO.getSkills());
        volunteerMapper.insert(volunteer);
    }
}
